package com.sf.dlb.service;

import com.sf.dlb.bean.UserShieldBean;
import com.sf.dlb.constant.CacheConstant;
import com.sf.dlb.constant.ResponseConstant;
import com.sf.dlb.mongo.model.NewList;
import com.sf.dlb.mongo.model.UserShield;
import com.sf.dlb.mongo.repository.UserShieldRepository;
import com.sf.dlb.utils.DateUtils;
import com.sf.dlb.utils.GsonUtils;
import com.sf.dlb.utils.ResultVOUtil;
import com.sf.dlb.vo.ResultVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by jing.tan on 2018/5/21.
 */
@Service
public class UserShieldService extends BaseService{
    private static Logger logger = LoggerFactory.getLogger(UserShieldService.class);

    @Autowired
    private UserShieldRepository userShieldRepository;

    /**
     * 添加屏蔽
     * @return
     */
    @Transactional
    public ResultVO addShield(UserShieldBean userShieldBean){
        logger.info("addShield userShieldBean={}", GsonUtils.getInstance().toJson(userShieldBean));
        String userId=userShieldBean.getUserId();
        String shieldId=userShieldBean.getShieldId();
        //并发操作
        String key= CacheConstant.USER_CACHE+CacheConstant.USER_SHIELD_PREFIX+userId+":"+shieldId;
        boolean concurrentFlag=isConcurrent(key);
        if(concurrentFlag){
            return ResultVOUtil.error(ResponseConstant.ERROR,"不能重复屏蔽");
        }
        UserShield userShield=new UserShield();
        userShield.setUserId(userShieldBean.getUserId());
        userShield.setSheildType(userShieldBean.getShieldType());
        userShield.setSheildId(userShieldBean.getShieldId());
        userShield.setSheildDesc(userShieldBean.getShieldDesc());
        userShield.setState("0");
        userShield.setCreateDate(new Date());
        userShield.setUpdateDate(new Date());
        userShieldRepository.save(userShield);
        return ResultVOUtil.success();
    }
    /**
     * 新闻屏蔽-过滤
     * @return
     */
    public List<NewList> newsShieldFilter(String userId, List<NewList> newListInfos){
        logger.info("newsShieldFilter start newListInfos.size={}",newListInfos.size());
        List<UserShield> userShields=userShieldRepository.findByUserIdAndSheildTypeAndDelFlag(userId,"1","0");
        if(CollectionUtils.isEmpty(userShields)){
            //没有屏蔽，就返回原来的list
            return newListInfos;
        }
        //有屏蔽，把之前的屏蔽内容移除
        List<NewList> filterNewsList=new ArrayList<>();
        for (NewList newList:newListInfos) {
            for (UserShield userShield:userShields) {
                if(!StringUtils.equals(userShield.getSheildId(),newList.getId())){
                    filterNewsList.add(newList);
                }
                break;
            }
        }
        logger.info("newsShieldFilter filterNewsList.size={}",filterNewsList.size());
        //remove 报异常 java.lang.UnsupportedOperationException
        return filterNewsList;
    }
    /**
     * 新闻屏蔽-定时任务触发
     * @return
     */
    @Transactional
    public void clearShield(){
        logger.info("clearShield start ");
        //清除15天之前的屏蔽数据了
        Date createDate=DateUtils.addDays(new Date(),-15);
        logger.info("clearShield createDate={}",createDate);
        List<UserShield> userShields=userShieldRepository.findByCreateDateBeforeAndDelFlag(createDate,"0");
        logger.info("clearShield  userShields.size={}",userShields.size());
        if(!CollectionUtils.isEmpty(userShields)){
            userShieldRepository.delete(userShields);
        }
    }
}
